﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using UpdateControllers;

namespace eTMSUpdateService
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string root = "";
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                root = System.IO.Path.GetFullPath(System.Configuration.ConfigurationSettings.AppSettings["UpdateRoot"]);
                CheckForIllegalCrossThreadCalls = false;
                Application.DoEvents();
                UpdateControllers.AutoUpdateService ctrl = new UpdateControllers.AutoUpdateService();
                ctrl.UpdateRoot = root;
                ctrl.OnStartUpdate += new OnFileDownLoadHandler(ctrl_OnStartUpdate);
                ctrl.OnFinishUpdate += new OnFileDownLoadHandler(ctrl_OnFinishUpdate);
                ctrl.OnUpdatedComponent += new OnFileDownLoadHandler(ctrl_OnUpdatedComponent);
                ctrl.OnUpdateError += new OnFileDownLoadHandler(ctrl_OnUpdateError);
                ctrl.OnBeginUpdateComponent += new OnFileDownLoadHandler(ctrl_OnBeginUpdateComponent);
                ctrl.OnUpdatedComponentPart += new OnFileDownLoadHandler(ctrl_OnUpdatedComponentPart);
                ctrl.OnConnectError += new EventHandler(ctrl_OnConnectError);
                Thread thread = new Thread(new ThreadStart(ctrl.CheckAllUpdate));
                thread.Start();
            }
            catch
            {
                StartApp();
                Application.Exit();
            }
        }

        void ctrl_OnConnectError(object sender, EventArgs e)
        {
            MessageBox.Show("Không thể kết nối đến máy chủ!");
            Application.Exit();
        }

        void ctrl_OnUpdatedComponentPart(object FileName, int Count)
        {
            progressBar1.Value++;
        }

        void ctrl_OnBeginUpdateComponent(object FileName, int Count)
        {
            progressBar1.Minimum = 0;
            progressBar1.Value = 0;
            progressBar1.Maximum = Count;
            lblAction.Text = "Đang cập nhật " + FileName.ToString() + "...";
        }

        void ctrl_OnUpdateError(object FileName, int Count)
        {
            MessageBox.Show("Có lỗi xảy ra khi đang cập nhật '" + FileName.ToString() + "'");
        }

        void ctrl_OnUpdatedComponent(object FileName, int Count)
        {
            lblAction.Text = "Hoàn thành cập nhật " + FileName.ToString();
            lblCount.Text = progressBar1.Value.ToString() + "/" + progressBar1.Maximum.ToString();
        }

        void ctrl_OnFinishUpdate(object FileName, int Count)
        {
            StartApp();
        }
        private void StartApp()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo(root.TrimEnd('\\') + "\\" + System.Configuration.ConfigurationSettings.AppSettings["StartFile"]);
            startInfo.WorkingDirectory = root;
            Process.Start(startInfo);
            Application.Exit();
        }
        void ctrl_OnStartUpdate(object FileName, int Count)
        {
            List<string> ls = (List<string>)FileName;
            lblAction.Text = "Bắt đầu cập nhật...";
            progressBar1.Maximum = ls.Count;
            progressBar1.Minimum = 0;
            progressBar1.Value = 0;
        }
    }
}
